package com.tanhua.admin.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.db.Log;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @auther : 白月尘
 * @date : 13:33  2021/10/28
 */
public class LogListener {

    @Autowired
    private LogMapper logMapper;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "tanhua.log.queue"),
                    exchange = @Exchange(value = "tanhua.log.exchange",
                    type = ExchangeTypes.TOPIC),
                    key = "log.*"
            )
    )
    public void log(String message) {
        try {
            Map<String, String> map = JSON.parseObject(message, Map.class);
            //String userId = map.get("userId");
            Long userId=Long.valueOf(map.get("userId"));
            String type = map.get("type");
            String logTime = map.get("logTime");
            Log log=new Log(userId,logTime,type);
            logMapper.insert(log);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}
